package cn.juque.workflow.listener;

import cn.juque.workflow.api.enums.VariableEnum;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.delegate.TaskListener;

import java.util.Map;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>AbstractWorkflowLisenter</li>
 * </ul>
 * @date 2023-01-31 19:53:16
 **/
@Slf4j
public abstract class AbstractWorkflowListener implements IWorkflowListener {

    @Override
    public void notify(DelegateExecution execution) {
        this.create(execution);
        this.take(execution);
        this.end(execution);
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        this.createTask(delegateTask);
        this.assignmentTask(delegateTask);
        this.rejectTask(delegateTask);
        this.updateTask(delegateTask);
        this.completeTask(delegateTask);
        this.deleteTask(delegateTask);
        this.timeoutTask(delegateTask);
    }

    /**
     * 执行监听--启动
     *
     * @param execution DelegateExecution
     */
    public void create(DelegateExecution execution) {
        if (!ExecutionListener.EVENTNAME_START.equals(execution.getEventName())) {
            return;
        }
        log.info("实例ID:{},{}执行监听-启动", execution.getProcessInstanceId(), execution.getCurrentActivityName());
    }

    /**
     * 执行监听--执行
     *
     * @param execution DelegateExecution
     */
    public void take(DelegateExecution execution) {
        if (!ExecutionListener.EVENTNAME_TAKE.equals(execution.getEventName())) {
            return;
        }
        log.info("实例ID:{},{}执行监听-执行", execution.getProcessInstanceId(), execution.getCurrentActivityName());
    }

    /**
     * 执行监听--结束
     *
     * @param execution DelegateExecution
     */
    public void end(DelegateExecution execution) {
        if (!ExecutionListener.EVENTNAME_END.equals(execution.getEventName())) {
            return;
        }
        log.info("实例ID:{},{}执行监听-结束", execution.getProcessInstanceId(), execution.getCurrentActivityName());
    }

    /**
     * 任务监听-开始
     *
     * @param delegateTask DelegateTask
     */
    public void createTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_CREATE.equals(delegateTask.getEventName())) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-开始", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }

    /**
     * 任务监听-处理
     *
     * @param delegateTask DelegateTask
     */
    public void assignmentTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_ASSIGNMENT.equals(delegateTask.getEventName())) {
            return;
        }
        // 驳回单独监听
        Map<String, Object> map = delegateTask.getVariables();
        if(null == map ||
                !VariableEnum.TASK_STATUS.getValue().equals(map.get(VariableEnum.TASK_STATUS.getValue()))) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-处理", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }

    /**
     * 任务监听-驳回
     *
     * @param delegateTask DelegateTask
     */
    public void rejectTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_ASSIGNMENT.equals(delegateTask.getEventName())) {
            return;
        }
        Map<String, Object> map = delegateTask.getVariables();
        if(null == map) {
            return;
        }
        if(!VariableEnum.TASK_STATUS.getValue().equals(map.get(VariableEnum.TASK_STATUS.getValue()))) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-驳回", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }

    /**
     * 任务监听-更新
     *
     * @param delegateTask DelegateTask
     */
    public void updateTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_UPDATE.equals(delegateTask.getEventName())) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-更新", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }

    /**
     * 任务监听-完成
     *
     * @param delegateTask DelegateTask
     */
    public void completeTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_COMPLETE.equals(delegateTask.getEventName())) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-完成", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }

    /**
     * 任务监听-废弃
     *
     * @param delegateTask DelegateTask
     */
    public void deleteTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_DELETE.equals(delegateTask.getEventName())) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-废弃", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }

    /**
     * 任务监听-超时
     *
     * @param delegateTask DelegateTask
     */
    public void timeoutTask(DelegateTask delegateTask) {
        if (!TaskListener.EVENTNAME_TIMEOUT.equals(delegateTask.getEventName())) {
            return;
        }
        log.info("实例ID:{},{},{}任务监听-超时", delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getName());
    }
}
